package com.ywjh.inter.service;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.LinkedList;
import java.util.List;

import com.ywjh.inter.conn.DBConnection;
import com.ywjh.inter.dao.PatientSportRecordDAO;
import com.ywjh.inter.dao.SportPlanDAO;
import com.ywjh.inter.dao.SportPlanItemDAO;
import com.ywjh.inter.dao.SportPlanItemTemplateDAO;
import com.ywjh.inter.dao.SportPlanTemplateDAO;
import com.ywjh.inter.entity.PatientSportRecordEntity;
import com.ywjh.inter.entity.SportPlanEntity;
import com.ywjh.inter.entity.SportPlanItemEntity;
import com.ywjh.inter.entity.SportPlanItemTemplateEntity;
import com.ywjh.inter.entity.SportPlanTemplateEntity;

public class SportService {
	private static SportService instance;
	private SportPlanDAO sportPlanDAO;
	private SportPlanItemDAO sportPlanItemDAO;
	private SportPlanTemplateDAO sportPlanTemplateDAO;
	private SportPlanItemTemplateDAO sportPlanItemTemplateDAO;
	private PatientSportRecordDAO patientSportRecordDAO;

	public static SportService getInstance() {
		if (instance == null) {
			instance=new SportService();
			instance.sportWay = new LinkedList<>();
			instance.sportWay.add("打太极");
			instance.sportWay.add("骑自行车");
			instance.sportWay.add("跳绳");
			instance.sportWay.add("快走");
			instance.sportWay.add("滑雪");
			instance.sportWay.add("慢跑");
			instance.sportWay.add("打羽毛球");
			instance.sportWay.add("爬楼梯");
			instance.sportWay.add("慢走");
			instance.sportWay.add("打篮球");
			instance.sportWay.add("打乒乓球");
			instance.sportWay.add("马拉松");
			instance.sportWay.add("踢足球");
			instance.sportWay.add("游泳");
			instance.sportWay.add("爬山");
			instance.sportWay.add("健身操");
			instance.sportWay.add("瑜伽");
			instance.sportWay.add("打高尔夫");

			instance.per = new LinkedList<>();
			instance.per.add("每周");
			instance.per.add("每天");

			instance.sportPlanDAO = new SportPlanDAO();
			instance.sportPlanItemDAO = new SportPlanItemDAO();
			instance.sportPlanTemplateDAO = new SportPlanTemplateDAO();
			instance.sportPlanItemTemplateDAO = new SportPlanItemTemplateDAO();
			instance.patientSportRecordDAO = new PatientSportRecordDAO();
			return instance;
		}
		return instance;
	}

	public List<String> getWay() {
		return sportWay;
	}

	public int addSportPlan(SportPlanEntity sportPlanEntity, List<SportPlanItemEntity> list) {
		Connection conn = null;
		int id = -1;
		try {
			conn = DBConnection.getDBConnection();
			conn.setAutoCommit(false);
			sportPlanDAO.disable(conn, sportPlanEntity.getPatient_id());
			id = sportPlanDAO.insert(conn, sportPlanEntity);
			if (id > 0) {
				for (SportPlanItemEntity itemEntity : list) {
					itemEntity.setSport_id(id);
					sportPlanItemDAO.insert(conn, itemEntity);
				}
			}
			conn.commit();
		} catch (ClassNotFoundException | SQLException e) {
			try {
				conn.rollback();
			} catch (SQLException e1) {
				e1.printStackTrace();
			}
			e.printStackTrace();
		} finally {
			try {
				conn.setAutoCommit(true);
			} catch (SQLException e) {
				e.printStackTrace();
			}
			DBConnection.close(conn);
		}
		return id;
	}

	public boolean addSportPlanTemplate(SportPlanTemplateEntity sportPlanEntity,
			List<SportPlanItemTemplateEntity> list) {
		boolean b = false;
		Connection conn = null;
		try {
			conn = DBConnection.getDBConnection();
			conn.setAutoCommit(false);
			int id = sportPlanTemplateDAO.insert(conn, sportPlanEntity);
			if (id > 0) {
				for (SportPlanItemTemplateEntity itemEntity : list) {
					itemEntity.setSport_id(id);
					sportPlanItemTemplateDAO.insert(conn, itemEntity);
				}
				b = true;
			}
			conn.commit();
		} catch (ClassNotFoundException | SQLException e) {
			try {
				conn.rollback();
			} catch (SQLException e1) {
				e1.printStackTrace();
			}
			e.printStackTrace();
		} finally {
			try {
				conn.setAutoCommit(true);
			} catch (SQLException e) {
				e.printStackTrace();
			}
			DBConnection.close(conn);
		}
		return b;
	}
	
	public boolean updateSportPlanTemplate(SportPlanTemplateEntity sportPlanEntity,
			List<SportPlanItemTemplateEntity> list) {
		boolean b = false;
		Connection conn = null;
		try {
			conn = DBConnection.getDBConnection();
			conn.setAutoCommit(false);
			b = sportPlanTemplateDAO.update(conn, sportPlanEntity);
			if (b) {
				sportPlanItemTemplateDAO.delete(conn, sportPlanEntity.getId());
				for (SportPlanItemTemplateEntity itemEntity : list) {
					itemEntity.setSport_id(sportPlanEntity.getId());
					sportPlanItemTemplateDAO.insert(conn, itemEntity);
				}
				b = true;
			}
			conn.commit();
		} catch (ClassNotFoundException | SQLException e) {
			try {
				conn.rollback();
			} catch (SQLException e1) {
				e1.printStackTrace();
			}
			e.printStackTrace();
		} finally {
			try {
				conn.setAutoCommit(true);
			} catch (SQLException e) {
				e.printStackTrace();
			}
			DBConnection.close(conn);
		}
		return b;
	}

	public boolean updateSportPlan(SportPlanEntity sportPlanEntity, List<SportPlanItemEntity> list) {
		boolean b = false;
		Connection conn = null;
		try {
			conn = DBConnection.getDBConnection();
			conn.setAutoCommit(false);
			b = sportPlanDAO.update(conn, sportPlanEntity);
			if (b) {
				sportPlanItemDAO.delete(conn, sportPlanEntity.getId());
				for (SportPlanItemEntity itemEntity : list) {
					itemEntity.setSport_id(sportPlanEntity.getId());
					sportPlanItemDAO.insert(conn, itemEntity);
				}
			}
			conn.commit();
		} catch (ClassNotFoundException | SQLException e) {
			try {
				conn.rollback();
			} catch (SQLException e1) {
				e1.printStackTrace();
			}
			e.printStackTrace();
		} finally {
			try {
				conn.setAutoCommit(true);
			} catch (SQLException e) {
				e.printStackTrace();
			}
			DBConnection.close(conn);
		}
		return b;
	}

	private List<String> sportWay = null;
	private List<String> per = null;

	public void disableSportByPatient(int patient_id) {
		Connection conn = null;
		try {
			conn = DBConnection.getDBConnection();
			sportPlanDAO.disable(conn, patient_id);
		} catch (ClassNotFoundException | SQLException e) {
			e.printStackTrace();
		} finally {
			DBConnection.close(conn);
		}
	}

	public List<SportPlanTemplateEntity> listTemplateByDocId(int doc_id) {
		List<SportPlanTemplateEntity> list = null;
		Connection conn = null;
		try {
			conn = DBConnection.getDBConnection();
			list = sportPlanTemplateDAO.listByDocID(conn, doc_id);
		} catch (ClassNotFoundException | SQLException e) {
			e.printStackTrace();
		} finally {
			DBConnection.close(conn);
		}
		return list;
	}

	public List<SportPlanItemTemplateEntity> listItemTemplateByPlanId(int id) {
		List<SportPlanItemTemplateEntity> list = null;
		Connection conn = null;
		try {
			conn = DBConnection.getDBConnection();
			list = sportPlanItemTemplateDAO.listByPlanId(conn, id);
		} catch (ClassNotFoundException | SQLException e) {
			e.printStackTrace();
		} finally {
			DBConnection.close(conn);
		}
		return list;
	}

	public List<SportPlanEntity> listByPatientId(int patient_id, int doc_id) {
		List<SportPlanEntity> list = null;
		Connection conn = null;
		try {
			conn = DBConnection.getDBConnection();
			list = sportPlanDAO.listByPatientId(conn, patient_id, doc_id);
		} catch (ClassNotFoundException | SQLException e) {
			e.printStackTrace();
		} finally {
			DBConnection.close(conn);
		}
		return list;
	}

	public List<PatientSportRecordEntity> listSportPatientByPlanId(int sport_plan_id) {
		List<PatientSportRecordEntity> list = null;
		Connection conn = null;
		try {
			conn = DBConnection.getDBConnection();
			list = patientSportRecordDAO.listSportPatientByPlanId(conn, sport_plan_id);
		} catch (ClassNotFoundException | SQLException e) {
			e.printStackTrace();
		} finally {
			DBConnection.close(conn);
		}
		return list;
	}

	public int addPatientSportRecord(PatientSportRecordEntity patientSportRecordEntity) {
		int id = -1;
		Connection conn = null;
		try {
			conn = DBConnection.getDBConnection();
			id = patientSportRecordDAO.insert(conn, patientSportRecordEntity);
		} catch (ClassNotFoundException | SQLException e) {
			e.printStackTrace();
		} finally {
			DBConnection.close(conn);
		}
		return id;
	}

	public PatientSportRecordEntity findPatientSportRecordById(int id) {
		PatientSportRecordEntity entity=null;
		Connection conn = null;
		try {
			conn = DBConnection.getDBConnection();
			entity=patientSportRecordDAO.findById(conn, id);
		}catch (Exception e) {
			e.printStackTrace();
		}finally {
			DBConnection.close(conn);
		}
		return entity;
	}

	public boolean updatePatientSportRecord(PatientSportRecordEntity patientSportRecordEntity) {
		boolean b=false;
		Connection conn = null;
		try {
			conn = DBConnection.getDBConnection();
			b = patientSportRecordDAO.update(conn, patientSportRecordEntity);
		} catch (ClassNotFoundException | SQLException e) {
			e.printStackTrace();
		} finally {
			DBConnection.close(conn);
		}
		return b;
	}

	public List<SportPlanEntity> listByPatientId(int patient_id) {
		List<SportPlanEntity> list=null;
		Connection conn = null;
		try {
			conn = DBConnection.getDBConnection();
			list = sportPlanDAO.listByPatientId(conn, patient_id);
		} catch (ClassNotFoundException | SQLException e) {
			e.printStackTrace();
		} finally {
			DBConnection.close(conn);
		}
		return list;
	}

	public List<PatientSportRecordEntity> listPatientRecordsByPlanId(int sport_plan_id, int patient_id) {
		List<PatientSportRecordEntity> list=null;
		Connection conn = null;
		try {
			conn = DBConnection.getDBConnection();
			list = patientSportRecordDAO.listPatientRecordsByPlanId(conn, sport_plan_id,patient_id);
		} catch (ClassNotFoundException | SQLException e) {
			e.printStackTrace();
		} finally {
			DBConnection.close(conn);
		}
		return list;
	}

	public List<SportPlanItemEntity> listItemByPlanId(int id) {
		List<SportPlanItemEntity> list = null;
		Connection conn = null;
		try {
			conn = DBConnection.getDBConnection();
			list = sportPlanItemDAO.listByPlanId(conn, id);
		} catch (ClassNotFoundException | SQLException e) {
			e.printStackTrace();
		} finally {
			DBConnection.close(conn);
		}
		return list;
	}

	public List<String> getPer() {
		return per;
	}
}
